package Servlet;


import BaseQuery.AddressQuery;
import Bean.Address;
import Server.IAddressServe;
import Server.impl.AddressService;
import Utils.IOUtils;
import Utils.JsonUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import vo.ResultBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/address")
public class AddressServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("---------AddressServlet层----------");
        //获取我们要执行的业务标志词
        String what = req.getParameter("what");
        //实例化一个BusinessService对象
        IAddressServe addressService = new AddressService() {
        };
        //声明最后响应的对象
        ResultBean resultBean = new ResultBean();
        try {
            if ("add".equals(what)) {
                System.out.println("------------添加地址--------------");

                //读取请求体中的json数据   转换成字符串
                String jsonStr = IOUtils.readStr(req.getInputStream());

                //将字符串转换成实体类对象
                Address address = JsonUtils.jsonStrToObj(jsonStr, null, Address.class);

                resultBean = addressService.add(address);
            }else if("update".equals(what)){
                System.out.println("------------修改地址--------------");

                //读取请求体中的json数据   转换成字符串
                String jsonStr = IOUtils.readStr(req.getInputStream());

                //将字符串转换成实体类对象
                Address address = JsonUtils.jsonStrToObj(jsonStr, null, Address.class);

                if("男".equals(address.getContactSex())){
                    address.setContactSex("1");
                }
                if("女".equals(address.getContactSex())){
                    address.setContactSex("0");
                }
                resultBean = addressService.update(address);
            }else if("del".equals(what)){
                System.out.println("------------删除地址--------------");
                //读取请求体中的id
                String id = req.getParameter("id");
                resultBean = addressService.delete(Integer.parseInt(id));
            }else if("getById".equals(what)){
                System.out.println("-----------通过id获取地址信息-------------");
                String id = req.getParameter("id");
                resultBean = addressService.getById(Integer.parseInt(id));
            }else if("page".equals(what)){
                System.out.println("----------分页查询地址------------");
                String jsonStr = IOUtils.readStr(req.getInputStream());

                AddressQuery addressQuery = JsonUtils.jsonStrToObj(jsonStr,null, AddressQuery.class);

                resultBean = addressService.getByPage(addressQuery);
            } else if("batchDel".equals(what)) {
                String jsonStr = IOUtils.readStr(req.getInputStream());

                JSONArray jsonArray=JSONObject.parseArray(jsonStr);
                List<Integer> ids=jsonArray.toJavaList(Integer.class);

                System.out.println("json转集合"+ids);
                resultBean=addressService.batchDel(ids);
            }
            else{
                System.out.println("--------无服务----------");
                resultBean = ResultBean.ERROR("no service");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultBean = ResultBean.ERROR(e.getMessage());
        }
        String resultStr = JSONObject.toJSONString(resultBean);

        resp.getWriter().write(resultStr);
    }
}
